package com.verykim.video;

import java.io.UnsupportedEncodingException;

public class HtmlRender {

	// final long currentTimeMillis = System.currentTimeMillis() + 86400 * 1000;

	private final StringBuilder body;
	private final HtmlCache hc;

	HtmlRender(final String basePath) {
		hc = new HtmlCache(basePath);

		body = new StringBuilder("<body>");
	}

	public String Render() {

		final StringBuilder html = new StringBuilder();

		html.append("<!DOCTYPE html>");
		html.append("<html>");
		html.append(hc.HeadHtml());

		body.append("<div id=\"footerbar\"><span>&copy2013 verykim</span><span><a href=\"javascript:void(0);\">服务协议</a></span><span><a href=\"javascript:void(0);\">隐私声明</a></span>");
		body.append("<span><a href=\"javascript:void(0);\">广告服务</a></span><span><script type=\"text/javascript\" src=\"http://js.tongji.linezing.com/1150668/tongji.js\"></script><noscript><img src=\"http://img.tongji.linezing.com/1150668/tongji.gif\"/></noscript></span>");
		body.append("<span><script type=\"text/javascript\">var _bdhmProtocol = ((\"https:\" == document.location.protocol) ? \" https://\" : \" http://\");document.write(unescape(\"%3Cscript src='\" + _bdhmProtocol + \"hm.baidu.com/h.js%3F3eaa47b5552423ed6ecc88b2219f48f4' type='text/javascript'%3E%3C/script%3E\"));</script></span><p>本站仅提供低品质影视欣赏方面的信息，电影版权归相关电影公司所有！</p></div>");

		body.append("</body></html>");

		html.append(body);

		return html.toString();
	}

	public void IndexPage(final String beginDate)
			throws UnsupportedEncodingException {
		body.append(hc.NavigateHtml(null, null));
		body.append("<div id=\"wrapper\">");
		try {
			body.append(hc.VideoListHtml(null, null, null, false, beginDate, 8));
			body.append(hc.SidebarHtml(null));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		body.append("</div>");
	}

	public void TodayPage() throws UnsupportedEncodingException {
		hc.TITLE = "今日更新 - " + hc.TITLE;
		body.append(hc.NavigateHtml(null, null));
		body.append("<div id=\"wrapper\">");
		try {
			body.append(hc.VideoListHtml(null, null, null, false, "today", 24));
			body.append(hc.SidebarHtml(null));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		body.append("</div>");
	}

	public void TonightPage() throws UnsupportedEncodingException {
		hc.TITLE = "今晚看啥 - " + hc.TITLE;
		body.append(hc.NavigateHtml(null, null));
		body.append("<div id=\"wrapper\">");
		try {
			body.append(hc.VideoListHtml(null, null, null, true, null, 24));
			body.append(hc.SidebarHtml(null));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		body.append("</div>");
	}

	public void CategoryPage(final String category, final String period,
			final String area, final String beginDate)
			throws UnsupportedEncodingException {

		body.append(hc.NavigateHtml(category, null));
		body.append("<div id=\"wrapper\">");
		try {
			body.append(hc.VideoListHtml(category, period, area, false,
					beginDate, 8));
			body.append(hc.SidebarHtml(category));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		body.append("</div>");
	}

	public void VideoPage(final String category, final String title)
			throws UnsupportedEncodingException {
		body.append(hc.NavigateHtml(category, null));
		body.append("<div id=\"wrapper\">");
		try {
			body.append(hc.VideoHtml(category, title));
			body.append(hc.SidebarHtml(category));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		body.append("</div>");
	}

	public void SearchPage(final String kw) throws UnsupportedEncodingException {

		body.append(hc.NavigateHtml(null, kw));
		body.append("<div id=\"wrapper\">");
		try {
			body.append(hc.SearchListHtml(kw));
			body.append(hc.SidebarHtml(null));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		body.append("</div>");
	}

}
